<?php

namespace app\admin\dao;

use app\common\model\LawCaseCategory;
use app\common\model\ProfessionalQa;
use ShidianUtil\Thinkphp\Dao\DaoTrait;

/**
 * ProfessionalQaDao 数据访问对象
 * Class ProfessionalQaDao
 * @package app\dao
 * @method ProfessionalQa findByPK(?int $id, $field = true)
 * @method \think\Collection selectAll(?int $page, ?int $page_size, $field = true)
 * @method ProfessionalQa insertActive(array $params, array $allow_field = [])
 * @method ProfessionalQa updateActive(ProfessionalQa $model, array $params, array $allow_field = [])
 * @method ProfessionalQa findAndUpdateActive(int $id, array $params, array $allow_field = [])
 * @method ProfessionalQa updateActiveByClosure(\Closure $closure, array $params, array $allow_field = [])
 * @method void deleteModel(ProfessionalQa $model)
 * @method void deleteById(int|array|\Closure $id, bool $force = false)
 * @link https://www.kancloud.cn/manual/thinkphp6_0/1037579
 * @link https://www.kancloud.cn/manual/think-orm/1257998
 * @link https://www.kancloud.cn/thinkphp/master-database-and-model/265551
 */
class ProfessionalQaDao
{
    use DaoTrait;

    /**
     * @var \app\common\model\ProfessionalQa
     */
    private $baseModel;

    /**
     * ProfessionalQaDao constructor.
     */
    public function __construct()
    {
        $this->baseModel = ProfessionalQa::class;
    }

    /**
     * @param $categoryId
     * @param $startTime
     * @param $endTime
     * @param $kw
     * @param int|null $page
     * @param int|null $page_size
     * @return array
     */
    public function listProfessionalQas($isEnd, $categoryId, $startTime, $endTime, $kw, ?int $page, ?int $page_size): array
    {
        $category_table_name = LawCaseCategory::getTable();

        $query = ProfessionalQa::when(is_numeric($isEnd), fn($query) => $query->where('isEnd', $isEnd))
            ->when(is_id($categoryId), fn($query) => $query->where('categoryId', $categoryId))
            ->when(is_numeric($startTime), fn($query) => $query->where('created', '>=', $startTime))
            ->when(is_numeric($endTime), fn($query) => $query->where('created', '<=', $endTime))
            ->when(not_blank($kw), fn($query) => $query->search('id|title', $kw))
            ->with([
                'category' => fn($query) => $query->tableField(['id', 'name'], $category_table_name),
                'user' => fn($query) => $query->shortUser(),
                'answerMember' => fn($query) => $query->shortMember(),
            ])
            ->order('id', 'desc')
            ->paging($page, $page_size);

        return listOutput($query, $page_size);
    }

    public function findProfessionalQa($id): ?ProfessionalQa
    {
        $category_table_name = LawCaseCategory::getTable();

        return ProfessionalQa::where(is_id($id), fn($query) => $query->where('id', $id))
            ->with([
                'category' => fn($query) => $query->tableField(['id', 'name'], $category_table_name),
                'user' => fn($query) => $query->shortUser(),
            ])
            ->find();
    }
}
